package com.zyg.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zyg.serviceImpl.LoginServiceImpl;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取账户密码，传递到业务层
		String username = request.getParameter("username");
		String password = request.getParameter("psd");
		LoginServiceImpl loginService = new LoginServiceImpl();
		boolean flag = loginService.login(username,password);
		//判断登录是否成功
		if (flag) {
			//成功的话将账户信息存入session，同时跳转到主页面
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			response.sendRedirect(request.getContextPath()+"/admin.jsp");
		}else {
			//失败的话返回登录页面，并给用户以提示
			request.setAttribute("error", "账户或密码错误");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
		
	}

}
